package com.probiz.estoremf.catalog.web.action;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.servlet.ModelAndView;

import com.probiz.estore.Constants;
import com.probiz.estore.catalog.service.VirtualCategoryManager;
import com.probiz.estore.common.model.catalog.VirtualCategory;
import com.probiz.estore.common.model.product.Product;
import com.probiz.estore.core.controller.GenericStoreFrontController;
import com.probiz.estore.core.model.BaseObject;
import com.probiz.estore.core.search.SearchCriteria;
import com.probiz.estore.product.model.ProductSearchCriteria;
import com.probiz.estore.product.service.ProdMainManager;
import com.probiz.estore.product.service.ProductManager;

public class VirtualCategoryFrontController extends	GenericStoreFrontController<VirtualCategory> {

	private ProductManager productManager = null;
	private VirtualCategoryManager virtualCategoryManager = null;
	private ProdMainManager prodMainManager = null;

	protected void initController() throws Exception {
		mgr = virtualCategoryManager;
	}

	public void setProductManager(ProductManager productManager) {
		this.productManager = productManager;
	}

	public void setProdMainManager(ProdMainManager prodMainManager) {
		this.prodMainManager = prodMainManager;
	}

	public void setVirtualCategoryManager(VirtualCategoryManager virtualCategoryManager) {
		this.virtualCategoryManager = virtualCategoryManager;
	}

	public ModelAndView defaultAction(HttpServletRequest request,
			HttpServletResponse response) {
		// -----start:获得虚拟目录------
		Integer categoryId = virtualCategoryManager.getCategoryIdByUri(request
				.getRequestURI());
		VirtualCategory virtualCategory = virtualCategoryManager.getById(categoryId);
		// 没有找到相应目录不能访问,直接进入找不到或已删除页面
		if (virtualCategory == null || !virtualCategory.getStatus().equals(Constants.FLAG_TRUE)) {
			try {
				response.sendError(404);
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}
		// -----end:获得虚拟目录------
		

		SearchCriteria searchCriteria = createSearchCriteria(request, BaseObject.FilterHqlName.DEFAULT);
		// ------start:改变页面显示条数 只有不是默认值的时候才需改变------
		if (searchCriteria.getPageSize() == defaultPageSize) {
			searchCriteria.changePaging(searchCriteria.getPageNo(), 10);
		}
		// ------end:改变页面显示条数 只有不是默认值的时候才需改变------

		// ------start:构造前台查询条件------
		ProductSearchCriteria productSearchCriteria = new ProductSearchCriteria();
		
		bindOnly(request, productSearchCriteria, "productSearchCriteria");
		productSearchCriteria.setStatus(Product.STATUS_ACTIVE);
		if(null == productSearchCriteria.getOrderby()){
			productSearchCriteria.setOrderby((short)0);
		}
		productSearchCriteria.setVirtualCategoryId(virtualCategory.getCategoryId());
		// ------end:构造前台查询条件------
		List<Product> productList = productManager.searchByCriteriaForController(productSearchCriteria,	searchCriteria);

		ModelAndView mv = new ModelAndView("product/virtualCategoryProductList");
		String loadMore = request.getParameter("loadMore");
		if(StringUtils.isNotEmpty(loadMore)){
			mv = new ModelAndView("product/include/loadMore");
		}
		mv.addObject("productList", productList);
		mv.addObject("virtualCategory", virtualCategory);
		mv.addObject("productSearchCriteria", productSearchCriteria);

		// ------start:把页面list的显示风格设置到attribute中------
		Cookie[] cookies = request.getCookies();
		if (!ArrayUtils.isEmpty(cookies)) {
			for (Cookie cookie : cookies) {
				if (cookie.getName().equals("listShowStyle")) {
					mv.addObject("listShowStyle", cookie.getValue());
				}
				if (cookie.getName().equals("introStyle")) {
					mv.addObject("introStyle", cookie.getValue());
				}
				if (cookie.getName().equals("compareCartStyle")) {
					mv.addObject("compareCartStyle", cookie.getValue());
				}
			}
		}
		// ------end:把页面list的显示风格设置到attribute中------
		
		// ------start:获得对比车上的商品------
		List<Product> compareProductList = prodMainManager.getCompareProductsFromRequest(request);
		mv.addObject("compareProductList",compareProductList);
		// ------end:  获得对比车上的商品------
		
		//标识显示的是促销产品
		mv.addObject("type", "virtual");
		return mv;
	}

}
